package com.amon.amonplayer.record;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.amon.amonplayer.R;
import com.amon.amonplayer.camera.widget.CameraFocusView;
import com.amon.amonplayer.camera.widget.CameraView;
import com.amon.amonplayer.record.widget.RecordProgressButton;

public class VideoRecordActivity extends AppCompatActivity implements BaseMediaRecorder.RecordListener {
    private CameraView mCameraView;
    private CameraFocusView mFocusView;
    private RecordProgressButton mRecordButton;
    private BaseMediaRecorder mVideoRecorder;
    private AssetManager mAssetManager;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_record);

        mCameraView = findViewById(R.id.camera_view);
        mFocusView = findViewById(R.id.camera_focus_view);
        mRecordButton = findViewById(R.id.record_button);
        mRecordButton.setMaxProgress(30000);

        mCameraView.setOnFocusListener(new CameraView.FocusListener() {
            @Override
            public void beginFocus(int x, int y) {
                mFocusView.beginFocus(x, y);
            }

            @Override
            public void endFocus(boolean success) {
                mFocusView.endFocus(success);
            }
        });

        mRecordButton.setOnRecordListener(new RecordProgressButton.RecordListener() {
            @Override
            public void onStart() {
                try {
                    mVideoRecorder = new DefaultMediaRecorder(VideoRecordActivity.this,
                            mCameraView.getEglContext(), mCameraView.getTextureId());
                    String filePath = getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/live_push.mp4";
                    Log.i("TAG", "保存视频到:" + filePath);

                    AssetFileDescriptor bgmDesc = mAssetManager.openFd("background.mp3");
                    mVideoRecorder.initVideo(bgmDesc, filePath, 720, 1280);
                    mVideoRecorder.setOnRecordListener(VideoRecordActivity.this);
                    mVideoRecorder.startRecord();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onEnd() {
                mVideoRecorder.stopRecord();
            }
        });

        mAssetManager = getAssets();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCameraView != null) {
            mCameraView.onDestroy();
        }
    }

    @Override
    public void onTime(long times) {
        mRecordButton.setCurrentProgress(times);
    }
}
